package com.cupster.greendaodemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cupster.func_db_greendao.crud.UserDaoHelper;
import com.cupster.func_db_greendao.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView mTvShow;

    UserDaoHelper mUserDaoHelper;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTvShow = findViewById(R.id.tv_show);
        findViewById(R.id.btn_add_item).setOnClickListener(this);
        findViewById(R.id.btn_add_items).setOnClickListener(this);
        findViewById(R.id.btn_delete_all).setOnClickListener(this);
        findViewById(R.id.btn_delete_item).setOnClickListener(this);
        findViewById(R.id.btn_update_item).setOnClickListener(this);
        findViewById(R.id.btn_query_all).setOnClickListener(this);
        findViewById(R.id.btn_query_by_id).setOnClickListener(this);
        findViewById(R.id.btn_query_item).setOnClickListener(this);
        findViewById(R.id.btn_query_where).setOnClickListener(this);

        mUserDaoHelper = new UserDaoHelper(getApplicationContext());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add_item:
                User user = new User();
                user.setAccount("18960222222");
                user.setName("Cupster");
                user.setPassword("Cupster@163.com");
                if (mUserDaoHelper.insert(user)){
                    Toast.makeText(this ,"已添加",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_add_items:
                List<User> list = new ArrayList<>();

                User user1 = new User();
                user1.setAccount("18960333333");
                user1.setName("CupsterXX");
                user1.setPassword("Cupster@163.com");
                User user2 = new User();
                user2.setAccount("18960444444");
                user2.setName("Div");
                user2.setDr(1);
                user2.setPassword("Cupster2@163.com");
                User user3 = new User();
                user3.setAccount("18960555555");
                user3.setName("Higloo");
                user3.setPassword("Cupster3@163.com");

                list.add(user1);
                list.add(user2);
                list.add(user3);

               if (mUserDaoHelper.insertMulti(list)){
                   Toast.makeText(this ,"已添加",Toast.LENGTH_SHORT).show();
               }
                break;
            case R.id.btn_delete_all:
                if (mUserDaoHelper.deleteAll()){
                    Toast.makeText(this ,"已删除",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_delete_item:
                User toDelelte =null;
                List <User> queryList = mUserDaoHelper.queryByQueryBuilder("18960222222");
                if (queryList != null && queryList.size() > 0 ){
                    toDelelte = queryList.get(0);
                    if (mUserDaoHelper.delete(toDelelte)){
                        Toast.makeText(this ,"已删除",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this ,"不存在or已删除",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_update_item:
                User toUpdate = null;
                List <User> queryList2 = mUserDaoHelper.queryByQueryBuilder("18960222222");
                if (queryList2 != null && queryList2.size() > 0 ){
                    toUpdate = queryList2.get(0);
                    toUpdate.setName(toUpdate.getName() +"[改]");
                }else {
                    toUpdate = new User();
                    toUpdate.setName("Xxx");
                    toUpdate.setAccount("133333333333");
                    toUpdate.setPassword("a123456");
                }
                if (mUserDaoHelper.update(toUpdate)){
                    Toast.makeText(this ,"已修改",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_query_all:
                List<User> users = mUserDaoHelper.queryAll();
                StringBuilder strBuilder = new StringBuilder();
                for (User item : users) {
                    strBuilder.append("\n");
                    strBuilder.append(item.get_id());
                    strBuilder.append("\n");
                    strBuilder.append(item.getName());
                    strBuilder.append("\n");
                    strBuilder.append(item.getAccount());
                }
                mTvShow.setText(strBuilder.toString());
                break;
            case R.id.btn_query_by_id:
                User userById = mUserDaoHelper.queryById(1);
                StringBuilder strBuilder1 = new StringBuilder();
                strBuilder1.append("\n");
                strBuilder1.append("查询id=1账户:");
                strBuilder1.append("\n");
                strBuilder1.append(userById.get_id());
                strBuilder1.append("\n");
                strBuilder1.append(userById.getName());
                strBuilder1.append("\n");
                strBuilder1.append(userById.getAccount());
                mTvShow.setText(strBuilder1.toString());
                break;
            case R.id.btn_query_item:
                String sql = "where _id > ?";
                String[] condition = new String[]{"0"};
                List<User> queryBySQL = mUserDaoHelper.queryBySQL(sql, condition);
                StringBuilder strBuilder2 = new StringBuilder();
                strBuilder2.append("\n");
                strBuilder2.append("查询id>0,账户:");
                for (User u : queryBySQL){
                    strBuilder2.append("\n");
                    strBuilder2.append(u.get_id());
                    strBuilder2.append("\n");
                    strBuilder2.append(u.getName());
                    strBuilder2.append("\n");
                    strBuilder2.append(u.getAccount());
                }
                mTvShow.setText(strBuilder2.toString());
                break;
            case R.id.btn_query_where:
                List<User> userByWhereList = mUserDaoHelper.queryByQueryBuilder(1);
                StringBuilder strBuilder3 = new StringBuilder();
                strBuilder3.append("\n");
                strBuilder3.append("查询dr=1,假删除账户:");
                for (User u : userByWhereList){
                    strBuilder3.append("\n");
                    strBuilder3.append(u.get_id());
                    strBuilder3.append("\n");
                    strBuilder3.append(u.getName());
                    strBuilder3.append("\n");
                    strBuilder3.append(u.getAccount());
                }
                mTvShow.setText(strBuilder3.toString());
                break;
            default:
                break;
        }
    }
}